package com.iflytek.web.sevlet;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.iflytek.domain.User;
import com.iflytek.service.UserService;
import com.iflytek.utils.VerifyCodeUtils;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/login")
public class LoginServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;

	public LoginServlet() {
		super();
	}

	// 将登陆验证码发送到session域当中
	public void sendVerifyCode(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		response.setContentType("image/jpeg");
		// 生成随机字串
		String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
		// 存入会话session
		HttpSession session = request.getSession(true);
		session.setAttribute("rand", verifyCode.toLowerCase());
		// 生成图片
		int w = 70, h = 35;
		VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode);
	}

	// ajax异步校验数据库中是否存在该手机号码
	public void isMobile(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ServletContext sc = request.getServletContext();
		WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sc);
		UserService us=(UserService) ac.getBean("userService");
		String phone = request.getParameter("userphone");
		boolean isExist = us.findPhoneNumberIsRegisted(phone);

		String json = "{\"isExist\":" + isExist + "}";
		response.getWriter().write(json);
	}

	// 校验登陆验证码是否正确
	public void verificationCode(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String code = request.getParameter("logincode");
		String code1 = (String) request.getSession().getAttribute("rand");
		boolean isExist = true;
		String code2 = code.toLowerCase();
		if (code != null && code1 != null) {
			if (code1.equals(code2)) {
				isExist = true;
			} else {
				//isExist = false;为方便调试验证码不管全为true
				isExist = true;
			}
		} else {
			//isExist = false;
			isExist = true;
		}
		String json = "{\"isExist\":" + isExist + "}";
		response.getWriter().write(json);
	}

	// 校验手机号与密码是否匹配
	public void verification(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ServletContext sc = request.getServletContext();
		WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sc);
		UserService us=(UserService) ac.getBean("userService");
		HttpSession session = request.getSession();
		String phone = request.getParameter("userphone");
		String password = request.getParameter("password");
		boolean isExist = us.findUserIsVerification(phone, password);
		System.out.println("sevlet" + isExist);
		String json = "{\"isExist\":" + isExist + "}";
		response.getWriter().write(json);
		if (isExist) {
			User user = us.getUserByPhoneNumber(phone);
			String autoLogin = request.getParameter("autoLogin");
			if ("autoLogin".equals(autoLogin)) {
				// String username_code=URLEncoder.encode(phone, "UTF-8");
				Cookie cookie_userphone = new Cookie("cookie_username", user.getUserphone());
				cookie_userphone.setMaxAge(10 * 60);
				Cookie cookie_password = new Cookie("cookie_password", user.getPassword());
				cookie_password.setMaxAge(10 * 60);
				response.addCookie(cookie_userphone);
				response.addCookie(cookie_password);
			}
			session.setAttribute("user", user);
			// request.getRequestDispatcher("/index.jsp").forward(request,
			// response);
			response.sendRedirect(request.getContextPath() + "/index.jsp");
		} else {
			request.setAttribute("loginError", "用户名或密码错误");
			request.getRequestDispatcher("/login.jsp").forward(request, response);
			// response.sendRedirect(request.getContextPath()+"/login.jsp");
		}
	}

	// 注销用户登录
	public void cancellation(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		session.removeAttribute("user");
		response.sendRedirect(request.getContextPath() + "/index.jsp");
	}

}
